/*
 * readSetupFile.java
 *
 * Created on January 8, 2006, 12:05 AM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

/**
 *
 * @author Sujal Bista
 */

import java.net.*;
import java.io.*;

public class readSetupFile 
{
    //server Address
    private InetAddress serverAddress;
    
    //server connecting port number
    private int portNum;
   
    /** Creates a new instance of readSetupFile */
    public readSetupFile() 
    {
        String fileName="setup.ini";
        String attrib;
        String value;        
        String fileContent;
        int index;
        try
        {
            System.out.println("Opening setup file : "+ fileName);
            BufferedReader inputScript=new BufferedReader(new FileReader(fileName));
            
            do
            {
                fileContent=inputScript.readLine();
                if(fileContent==null)
                    break;
                
                
                index=fileContent.indexOf("//");
                if(index!=-1)
                {
                     fileContent=fileContent.substring(0, index);
                }
                    
                index=fileContent.indexOf('=');
                
                if(index<0)
                    continue;
                
                attrib=fileContent.substring(0, index);
                value=fileContent.substring(index+2);
                
                if(attrib.indexOf("Server Address")!=-1)
                {
                    System.out.println(attrib+" : "+value);
                    serverAddress = InetAddress.getByName(value);
                }
                else if(attrib.indexOf("Server IP")!=-1)
                {
                    System.out.println(attrib+" : "+value);
                    serverAddress = InetAddress.getByName(value);
                }
                else if(attrib.indexOf("Port No")!=-1)
                {
                    System.out.println(attrib+" : "+value);
                    portNum=Integer.valueOf(value);
                }
                else if(attrib.indexOf("Default Judge Num")!=-1)
                {
                    System.out.println(attrib+" : "+value);
                    globalData.defaultJudgeType=Integer.valueOf(value);
                    globalData.judgeType=globalData.judgeNumberToJudgeType(globalData.defaultJudgeType);
                }
            }while(true);
           
            System.out.println("Closing setup file : "+ fileName+"\n");
            inputScript.close();
        }catch(FileNotFoundException e)
        {
              System.out.println("Error : File \""+fileName+"\" not found. ");
              System.exit(1);
        }
        catch(IOException e)
        {
            System.out.println("Error : "+e.getMessage());
            System.exit(1);
        }
        
    }
    
    //returns server name
    public InetAddress getServerName()
    {
        return serverAddress;
    }
    
    //returns port number
    public int getPortNum()
    {
        return portNum;
    }
    
}
